package com.baizhiedu.rsm.controller;


import com.baizhiedu.rsm.entity.*;
import com.baizhiedu.rsm.service.ScheduleServcie;
import com.baizhiedu.rsm.service.StudentMessageServcie;
import com.baizhiedu.rsm.service.StudentServcie;
import com.baizhiedu.rsm.service.SuggestService;
import com.baizhiedu.rsm.util.DateConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author lby
 */
@Controller
@RequestMapping("schedule")
public class ScheduleController {
    @Autowired
    private ScheduleServcie scheduleServcie;
    @Autowired
    private StudentMessageServcie studentMessageServcie;
    @Autowired
    private SuggestService suggestService;
    /**
     * 获取所有的进度信息
     * @return
     */
    @RequestMapping("showall")
    @ResponseBody
    public List<Schedule> showAll(){
        return scheduleServcie.showAll();
    }

    /**
     * 1.添加进度到对应学生的信息中
     * 2.添加学生意见至意见表
     * 3.获取题目信息存入Session
     * @param ids
     * @param session
     * @param suggest
     * @return
     */
    @RequestMapping("addSchedule")
    public String addSchedule(Integer ids, HttpSession session, String suggest){
        //获取Session中的用户信息 拿到学生id 根据id查询学生的所有信息
        StudentMessage user = (StudentMessage) session.getAttribute("user");
        //调用addSchedule方法添加新进度的同时返回用户需要回答的题目
        List<Subject> subjects = studentMessageServcie.addSchedule(user.getId(), ids);
//        添加建议
        if(suggest != null && !"".equals(suggest)){
            Suggest suggest1 = new Suggest();
            suggest1.setXsname(user.getName());
            suggest1.setTime(new DateConverter().convert(DateFormat.getDateInstance().format(new Date())));
            suggest1.setText(suggest);
            suggestService.insertOne(suggest1);
        }
//        添加建议操作结束
        session.setAttribute("subjects",subjects);
        return "redirect:/showSubject.html";
    }

    /**
     * Session中获取用户信息 得到学生id  获取进度信息
     * @return
     */
    @RequestMapping("getSchedule")
    @ResponseBody
    public String getSchedule(HttpSession session){
        StudentMessage user = (StudentMessage) session.getAttribute("user");
        return user.getSchedule();
    }
}
